package org.corf.comm.jms;

import java.util.Map;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

/**
 * For the time being, the objects need to be passed already set up.
 */
public class RichProducer {

    public static class Builder {
        private String name;
        private Session session;
        private Destination destination;
        private String destinationUri;
        private MessageProducer producer;

        public Builder() {
        }

        void setName(String name) {
            this.name = name;
        }

        void setSession(Session session) {
            this.session = session;
        }

        void setDestination(Destination destination) {
            this.destination = destination;
        }

        void setDestinationUri(String destinationUri) {
            this.destinationUri = destinationUri;
        }

        void setProducer(MessageProducer producer) {
            this.producer = producer;
        }

        Builder withName(String name) {
            setName(name);
            return this;
        }

        Builder withSession(Session session) {
            setSession(session);
            return this;
        }

        Builder withDestination(Destination destination) {
            setDestination(destination);
            return this;
        }

        Builder withDestinationUri(String destinationUri) {
            setDestinationUri(destinationUri);
            return this;
        }

        Builder withProducer(MessageProducer producer) {
            setProducer(producer);
            return this;
        }

        RichProducer build() {
            RichProducer h = new RichProducer(name, session, destination, destinationUri, producer);
            return h;
        }
    }

    private final String name;
    private final Session session;
    private final Destination destination;
    private final String destinationUri;
    private final MessageProducer producer;

    /**
     * @param name
     * @param session
     * @param destination
     * @param destinationUri
     * @param producer
     */
    public RichProducer(String name, Session session, Destination destination, String destinationUri, MessageProducer producer) {
        this.name = name;
        this.session = session;
        this.destination = destination;
        this.destinationUri = destinationUri;
        this.producer = producer;
    }

    public String getName() {
        return name;
    }

    public String getDestinationUri() {
        return destinationUri;
    }

    public String sendMessage(Map<String, Boolean> booleanProperties, Map<String, Byte> byteProperties,
            Map<String, Double> doubleProperties, Map<String, Float> floatProperties, Map<String, Integer> integerProperties,
            Map<String, Long> longProperties, Map<String, Object> objectProperties, Map<String, Short> shortProperties,
            Map<String, String> stringProperties,
            String jmsCorrelationId, byte[] jmsCorrelationIdAsBytes, int jmsDeliveryMode, long jmsExpiration, int jmsPriority,
            boolean jmsRedelivered, Destination jmsReplyTo, long jmsTimestamp, String jmsType) throws JMSException {
        String messageId = null;
        Message m = session.createMessage();
        // Copy all passed JMS properties.
        //
        // Copy all passed properties.
        //

        producer.send(m);
        messageId = m.getJMSMessageID();
        return messageId;
    }

}
